import { Injectable } from '@angular/core';
import { Headers, Http, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
import { ResponseModel } from '../model/comm/response-model';
import { ReportUrls } from './report.urls.conf';
declare const $: any;

@Injectable()
export class UserService {
    private headers = new Headers({ 'Content-Type': 'application/json' });
    options = new RequestOptions({
        headers: this.headers
    });

    constructor(protected http: Http) {
    }

    login(username: string, password: string): Observable<any> {
        return this.http.get(ReportUrls.BaseUrl + '/' + ReportUrls.Login + '?userId=' + username + '&password=' + password, this.options)
            .catch(e => this.handleErrorObservable(e)).map(response => {
                const model: ResponseModel = JSON.parse(response._body);
                return model;
            });
    }

    login2(code: string): Observable<any> {
        return this.http.get(ReportUrls.BaseUrl + '/' + ReportUrls.PtLogin + '?code=' + code , this.options)
            .catch(e => this.handleErrorObservable(e)).map(response => {
                const model: ResponseModel = JSON.parse(response._body);
                return model;
            });
    }

    /**
     * 异常捕获
     * @param error exception catch
     */
    private handleErrorObservable(error: Response | any) {
        this.showNotification('数据服务请求失败', 'danger');
        return Observable.throw(error);
    }


    /**
     * 
     * @param message notifycation
     * @param notiType   'info', 'success', 'warning', 'danger', 'rose', 'primary'
     * @param align 
     */
    showNotification(message: any, notiType: string) {
        if (notiType === undefined) {
            notiType = 'info';
        }
        $.notify({
            icon: 'notifications',
            message: message
        }, {
                type: notiType,
                timer: 1000,
                delay: 1000,
                offset: {
                    y: 150,
                },
                z_index: 9999,
                placement: {
                    from: 'top',
                    align: 'center'
                }
            });
    }


}

